﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;
using Zwms.Client.InfoSys;

namespace Zwms.Client.OutWms
{
    public partial class SendForm : BaseForm
    {
        #region 成员变量
        private BilOutBill _outBill;
        private DateTime _dtnow = DateTime.Now;
        private string _sLastScanCode;
        private IList<BilOutBillDetail> _sendList;
        #endregion

        #region 构造函数
        public SendForm(BilOutBill outBill)
        {
            InitializeComponent();
            _outBill = outBill;
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnClearSingle.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnClearAll.Image = global::Zwms.Client.Properties.Resources.cancel2;
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void InitControl()
        {
            LoadPlanSendList();
        }
        //载入待发货物列表
        private void LoadPlanSendList()
        {
            lsvPlanSend.Items.Clear();
            ServiceReferenceZwms.ServiceZwmsClient client
                  = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutBillDetail queryRecord = new BilOutBillDetail();
            queryRecord.OBCode = _outBill.Id;
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            _sendList = client.QueryOutBillDetailList(queryRecord, ref pIndex);

            foreach (BilOutBillDetail record in _sendList)
            {
                ListViewItem lvItem = new ListViewItem(record.GoodsBarCode);
                lvItem.SubItems.Add(record.GoodsName);
                lvItem.SubItems.Add(record.SingleGoodsCode);
                lvItem.SubItems.Add(record.GoodsPackageQty.ToString());
                lvItem.SubItems.Add(record.GoodsPieceQty.ToString());
                lvItem.SubItems.Add(record.OutGoodsPackageQty.ToString());
                lvItem.SubItems.Add(record.OutGoodsPieceQty.ToString());

                //着色
                if ((record.OutGoodsPackageQty == record.GoodsPackageQty)
                    && (record.OutGoodsPieceQty == record.GoodsPieceQty))
                {
                    lvItem.BackColor = Color.Green;
                }
                else if ((record.OutGoodsPackageQty > record.GoodsPackageQty)
                    || (record.OutGoodsPieceQty > record.GoodsPieceQty))
                {
                    lvItem.BackColor = Color.Red;
                }
                else if ((record.OutGoodsPackageQty <= record.GoodsPackageQty)
                    && (record.OutGoodsPieceQty <= record.GoodsPieceQty))
                {
                    lvItem.BackColor = Color.Yellow;
                }

                lsvPlanSend.Items.Add(lvItem);
            }
        }

        //参数bIsScan表示是否为扫描结果
        private void SendGoods(string sGoodsCode, string sDate, int iPackageQty, int iPieceQty)
        {
            if (sGoodsCode == null)
            {
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
                 = new ServiceReferenceZwms.ServiceZwmsClient();
            bool bHave = false;

            if (sDate != "")
            {
                //整箱
                List<BilOutBillDetail> packageList =  _sendList.Where(c => c.GoodsBarCode == string.Format("{0}#{1}", sGoodsCode, sDate)).ToList();
                if (packageList.Count == 0)
                {
                    //计划之外
                }
                else if (packageList.Count == 1)
                {
                    bHave = true;
                    packageList[0].OutGoodsPackageQty += iPackageQty;
                    packageList[0].OutGoodsPieceQty += iPieceQty;
                    packageList[0].OutGoodsQty = packageList[0].OutGoodsPackageQty * packageList[0].GoodsSpec + packageList[0].OutGoodsPieceQty;

                    if (!client.UpdateOutBillDetail(packageList[0]))
                    {
                        MessageBox.Show("更新出库计划明细信息失败！");
                        return;
                    }
                }
                else if (packageList.Count >1 )
                {
                    bHave = true;
                    for (int j = 0; j < packageList.Count; j++)
                    {
                        if (packageList[j].OutGoodsPackageQty < packageList[j].GoodsPackageQty ||
                            packageList[j].OutGoodsPieceQty < packageList[j].GoodsPieceQty)
                        {
                            if (iPackageQty < (packageList[j].GoodsPackageQty - packageList[j].OutGoodsPackageQty))
                            {
                                packageList[j].OutGoodsPackageQty += iPackageQty;
                                iPackageQty = 0;
                            }
                            else if(iPackageQty == (packageList[j].GoodsPackageQty - packageList[j].OutGoodsPackageQty))
                            {
                                packageList[j].OutGoodsPackageQty += iPackageQty;
                                iPackageQty = 0;
                            }
                            else if (iPackageQty > (packageList[j].GoodsPackageQty - packageList[j].OutGoodsPackageQty))
                            {
                                iPackageQty = iPackageQty - (packageList[j].GoodsPackageQty - packageList[j].OutGoodsPackageQty);
                                packageList[j].OutGoodsPackageQty = packageList[j].GoodsPackageQty;
                            }

                            if (iPieceQty < (packageList[j].GoodsPieceQty - packageList[j].OutGoodsPieceQty))
                            {
                                packageList[j].OutGoodsPieceQty += iPieceQty;
                                iPieceQty = 0;
                            }
                            else if (iPieceQty == (packageList[j].GoodsPieceQty - packageList[j].OutGoodsPieceQty))
                            {
                                packageList[j].OutGoodsPieceQty += iPieceQty;
                                iPieceQty = 0;
                            }
                            else if (iPieceQty > (packageList[j].GoodsPieceQty - packageList[j].OutGoodsPieceQty))
                            {
                                iPieceQty = iPieceQty - (packageList[j].GoodsPieceQty - packageList[j].OutGoodsPieceQty);
                                packageList[j].OutGoodsPieceQty = packageList[j].GoodsPieceQty;
                            }

                            //计算总数
                            packageList[j].OutGoodsQty = packageList[j].OutGoodsPackageQty * packageList[j].GoodsSpec + packageList[j].OutGoodsPieceQty;
                            if (!client.UpdateOutBillDetail(packageList[j]))
                            {
                                MessageBox.Show("更新出库明细信息失败！");
                                return;
                            }
                        }
                        else
                        {
                            if (j != (packageList.Count - 1))
                            {
                                continue;
                            }
                            else
                            {
                                packageList[j].OutGoodsPackageQty += iPackageQty;
                                packageList[j].OutGoodsPieceQty  += iPieceQty;
                                packageList[j].OutGoodsQty = packageList[j].OutGoodsPackageQty * packageList[j].GoodsSpec + packageList[j].OutGoodsPieceQty;
                                if (!client.UpdateOutBillDetail(packageList[j]))
                                {
                                    MessageBox.Show("更新出库明细信息失败！");
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //零数
                List<BilOutBillDetail> pieceList = _sendList.Where(c => c.GoodsCode == sGoodsCode).ToList();
                if (pieceList.Count == 0)
                {
                    //计划之外
                }
                else if (pieceList.Count == 1)
                {
                    bHave = true;
                    pieceList[0].OutGoodsPackageQty += iPackageQty;
                    pieceList[0].OutGoodsPieceQty += iPieceQty;
                    pieceList[0].OutGoodsQty = pieceList[0].OutGoodsPackageQty * pieceList[0].GoodsSpec + pieceList[0].OutGoodsPieceQty;

                    if (!client.UpdateOutBillDetail(pieceList[0]))
                    {
                        MessageBox.Show("更新出库计划明细信息失败！");
                        return;
                    }
                }
                else if (pieceList.Count >1 )
                {
                    bHave = true;
                    for (int j = 0; j < pieceList.Count; j++)
                    {
                        if (pieceList[j].OutGoodsPackageQty < pieceList[j].GoodsPackageQty ||
                            pieceList[j].OutGoodsPieceQty < pieceList[j].GoodsPieceQty)
                        {
                            if (iPackageQty < (pieceList[j].GoodsPackageQty - pieceList[j].OutGoodsPackageQty))
                            {
                                pieceList[j].OutGoodsPackageQty += iPackageQty;
                                iPackageQty = 0;
                            }
                            else if (iPackageQty == (pieceList[j].GoodsPackageQty - pieceList[j].OutGoodsPackageQty))
                            {
                                pieceList[j].OutGoodsPackageQty += iPackageQty;
                                iPackageQty = 0;
                            }
                            else if (iPackageQty > (pieceList[j].GoodsPackageQty - pieceList[j].OutGoodsPackageQty))
                            {
                                iPackageQty = iPackageQty - (pieceList[j].GoodsPackageQty - pieceList[j].OutGoodsPackageQty);
                                pieceList[j].OutGoodsPackageQty = pieceList[j].GoodsPackageQty;
                            }

                            if (iPieceQty < (pieceList[j].GoodsPieceQty - pieceList[j].OutGoodsPieceQty))
                            {
                                pieceList[j].OutGoodsPieceQty += iPieceQty;
                                iPieceQty = 0;
                            }
                            else if (iPieceQty == (pieceList[j].GoodsPieceQty - pieceList[j].OutGoodsPieceQty))
                            {
                                pieceList[j].OutGoodsPieceQty += iPieceQty;
                                iPieceQty = 0;
                            }
                            else if (iPieceQty > (pieceList[j].GoodsPieceQty - pieceList[j].OutGoodsPieceQty))
                            {
                                iPieceQty = iPieceQty - (pieceList[j].GoodsPieceQty - pieceList[j].OutGoodsPieceQty);
                                pieceList[j].OutGoodsPieceQty = pieceList[j].GoodsPieceQty;
                            }

                            //
                            pieceList[j].OutGoodsQty = pieceList[j].OutGoodsPackageQty * pieceList[j].GoodsSpec + pieceList[j].OutGoodsPieceQty;

                            if (!client.UpdateOutBillDetail(pieceList[j]))
                            {
                                MessageBox.Show("更新出库计划明细信息失败！");
                                return;
                            }
                        }
                        else
                        {
                            if (j != (pieceList.Count - 1))
                            {
                                continue;
                            }
                            else
                            {
                                pieceList[j].OutGoodsPackageQty += iPackageQty;
                                pieceList[j].OutGoodsPieceQty += iPieceQty;
                                pieceList[j].OutGoodsQty = pieceList[j].OutGoodsPackageQty * pieceList[j].GoodsSpec + pieceList[j].OutGoodsPieceQty;
                                if (!client.UpdateOutBillDetail(pieceList[j]))
                                {
                                    MessageBox.Show("更新出库计划明细信息失败！");
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            if (!bHave)
            {
                //发货计划中无此项记录，不进行记录
                return;
            }
            LoadPlanSendList();
        }

        //从扫描的条码中获取货物条码  格式“货物条码#入库时间”
        private string GetGoodsCode(string sScanBarCode, ref string sDate)
        {
            if (sScanBarCode.Contains('#'))
            {
                List<string> sList = CommonHelper.splitStr(sScanBarCode, '#');
                if (sList.Count != 2)
                {
                    return null;
                }
                else
                {
                    sDate = sList[1];
                    return sList[0];
                }
            }
            else
            {
                foreach (BilOutBillDetail record in _sendList)
                {
                    if (record.SingleGoodsCode == sScanBarCode)
                    {
                        return record.GoodsCode;
                    }
                }


                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();
                BaseGood queryRecord = new BaseGood();
                queryRecord.SingleGoodsCode = sScanBarCode;
                PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
                IList<BaseGood> goodsList = client.QueryGoodsList(queryRecord, ref  pagerIndex);
                if (goodsList.Count > 0)
                {
                    return goodsList[0].Id;
                }
                else
                {
                    return null;
                }
            }
        }

        private void ResetNud()
        {
            nudPackageQty.Value = 0;
            nudPieceQty.Value = 0;
        }
        #endregion

        #region 控件函数
        private void tbBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 按下回车键
            if (e.KeyChar == '\r')
            {
                if (chbAutoSave.Checked)
                {
                    //当自动保存被选中，此时回车键为将结果自动添加
                    string sDate = "";
                    string sGoodsCode = GetGoodsCode(tbBarCode.Text, ref sDate);
                    if (tbBarCode.Text.Contains("#"))
                    {
                        SendGoods(sGoodsCode,sDate, 1, 0);
                    }
                    else
                    {
                        SendGoods(sGoodsCode,sDate, 0, 1);
                    }

                }
                else
                {
                    //调整数量
                    if (_sLastScanCode == tbBarCode.Text)
                    {
                        if (tbBarCode.Text.Contains("#"))
                        {
                            nudPackageQty.Value += 1;
                        }
                        else
                        {
                            nudPieceQty.Value += 1;
                        }
                    }
                    else
                    {
                        if (tbBarCode.Text.Contains("#"))
                        {
                            nudPackageQty.Enabled = true;
                            nudPackageQty.Value = 1;
                        }
                        else
                        {
                            nudPackageQty.Enabled = false;
                            nudPackageQty.Value = 0;
                            nudPieceQty.Value = 1;
                        }
                    }
                    _sLastScanCode = tbBarCode.Text;
                }
                e.Handled = true;
                tbBarCode.SelectAll();
            }
        }

        private void nudQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                if (!chbAutoSave.Checked)
                {
                    try
                    {
                        string sDate = "";
                        string sGoodsCode = GetGoodsCode(tbBarCode.Text, ref sDate);
                        SendGoods(sGoodsCode, sDate, Convert.ToInt32(nudPackageQty.Value), Convert.ToInt32(nudPieceQty.Value));
                        ResetNud();
                    }
                    catch
                    {
                        ResetNud();
                    }
                }
            }
        }

        private void chbAutoSave_CheckedChanged(object sender, EventArgs e)
        {
            nudPackageQty.Enabled = !chbAutoSave.Checked;
            nudPieceQty.Enabled = !chbAutoSave.Checked;
            tbBarCode.Text = string.Empty;
            if (chbAutoSave.Checked)
            {
                nudPackageQty.Value = 0;
                nudPieceQty.Value = 0;
            }
        }

        private void btnClearSingle_Click(object sender, EventArgs e)
        {
            if (lsvPlanSend.SelectedItems.Count > 0)
            {
                ListViewItem selectItem = lsvPlanSend.SelectedItems[0];


                ServiceReferenceZwms.ServiceZwmsClient client
                  = new ServiceReferenceZwms.ServiceZwmsClient();
                foreach (BilOutBillDetail record in _sendList)
                {
                    if (selectItem.Text == record.GoodsBarCode)
                    {
                        record.OutGoodsPackageQty = 0;
                        record.OutGoodsPieceQty = 0;
                        record.OutGoodsQty = 0;

                        if (!client.UpdateOutBillDetail(record))
                        {
                            MessageBox.Show("更新出库计划明细信息失败！");
                            return;
                        }
                    }
                }
                LoadPlanSendList();
            }
            else
            {
                MessageBox.Show("请首先选择要清空的收货记录！");
                return;
            }
        }

        private void btnEditSingle_Click(object sender, EventArgs e)
        {
            if (lsvPlanSend.SelectedItems.Count > 0)
            {
                ListViewItem selectItem = lsvPlanSend.SelectedItems[0];
                LoadPlanSendList();
            }
            else
            {
                MessageBox.Show("请首先选择要编辑的收货记录！");
                return;
            }
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("清空全部收货信息，是否继续？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
            ServiceReferenceZwms.ServiceZwmsClient client
                        = new ServiceReferenceZwms.ServiceZwmsClient();

            foreach (BilOutBillDetail record in _sendList)
            {
                record.OutGoodsPackageQty = 0;
                record.OutGoodsPieceQty = 0;
                record.OutGoodsQty = 0;

                if (!client.UpdateOutBillDetail(record))
                {
                    MessageBox.Show("更新入库计划明细信息失败！");
                    return;
                }
            }
            LoadPlanSendList();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("完成发货？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            //1.出库单状态改为“已完成”
            //2.将出库单中的发货信息同步到出库计划单中
            //3.判断出库计划单是否已完成
            //4.如果出库类型为移库出库，须在对应仓库中生成入库单和入库单明细

            //首先判断当前出库数量是否与拣货单相符合

            ServiceReferenceZwms.ServiceZwmsClient client
                      = new ServiceReferenceZwms.ServiceZwmsClient();
            BilOutBillDetail queryRecord = new BilOutBillDetail();
            queryRecord.OBCode = _outBill.Id;
            PagerIndex pIndex = new PagerIndex(1, int.MaxValue);
            IList<BilOutBillDetail> sendList = client.QueryOutBillDetailList(queryRecord, ref pIndex);
            bool bCanSend = true;
            bool bEnough = true;
            string stemp = "";
            foreach (BilOutBillDetail record in sendList)
            {
                if ((record.GoodsPackageQty < record.OutGoodsPackageQty)
                    || (record.GoodsPieceQty < record.OutGoodsPieceQty))
                {
                    //存在超出计划发货的情况，发货不能继续
                    bCanSend = false;
                    stemp += string.Format("货物{0}存在发货数量超限情况 \r\n", record.GoodsName);
                }

                if ((record.GoodsPackageQty > record.OutGoodsPackageQty)
                   || (record.GoodsPieceQty > record.OutGoodsPieceQty))
                {
                    //存在超出计划发货的情况，发货不能继续
                    bEnough = false;
                    stemp += string.Format("货物{0}存在发货数量不足情况 \r\n", record.GoodsName);
                    stemp += "是否继续？";
                }
            }

            if (!bCanSend)
            {
                MessageBox.Show(stemp);
                return;
            }
            if (!bEnough)
            {
                if (MessageBox.Show(stemp, "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
            }

            //执行发货
            //1.调整出库单状态
            _outBill.BillStatus = "2";
            _outBill.OutDate = string.Format("{0:yyyyMMdd}", client.GetCurrentSystemTime());
            if (client.UpdateOutBill(_outBill))
            {
                SysLogWriter.WriteOptLog("发货完成调整出库单状态", _outBill.Id, "改");
                //2.将在出库计划单中加入加入对应的数量，判断出库计划但是否完成，更新其状态
                BilOutPlanBill queryOutPlanBill = new BilOutPlanBill();
                queryOutPlanBill.Id = _outBill.OBPlanCode;
                queryOutPlanBill = client.QueryOutPlanBill(queryOutPlanBill);
                if (queryOutPlanBill != null)
                {
                    BilOutPlanBillDetail queryOutPlanBillDetail = new BilOutPlanBillDetail();
                    queryOutPlanBillDetail.OBPlanCode = queryOutPlanBill.Id;
                    IList<BilOutPlanBillDetail> outPlanBillDetailList = client.QueryOutPlanBillDetailList(queryOutPlanBillDetail, ref pIndex);
                    for (int i = 0; i < outPlanBillDetailList.Count; i++)
                    {
                        BilOutPlanBillDetail outPlanBillDetailRecord = outPlanBillDetailList[i];
                        foreach (BilOutBillDetail record in sendList)
                        {
                            //判断发货列表中的项与发货计划中的对应项，根据货物编码进行联系
                            if (outPlanBillDetailRecord.GoodsCode == record.GoodsCode)
                            {
                                //如相同，则在发货计划中计划实际发货的数量
                                outPlanBillDetailRecord.OutGoodsPackageQty += record.OutGoodsPackageQty;
                                outPlanBillDetailRecord.OutGoodsPieceQty += record.OutGoodsPieceQty;
                                outPlanBillDetailRecord.OutGoodsQty += record.OutGoodsQty;
                            }
                        }
                        if (!client.UpdateOutPlanBillDetail(outPlanBillDetailRecord))
                        {
                            MessageBox.Show("更新出库计划明细失败！");
                        }
                    }//经过遍历后，判断发货计划中是否已经完全发货
                    string sTemp = "";
                    if (outPlanBillDetailList.Where(c => c.GoodsPackageQty > c.OutGoodsPackageQty || c.GoodsPieceQty > c.OutGoodsPieceQty).Count() > 0)
                    {
                        //表示发货计划尚未完成
                        queryOutPlanBill.BillStatus = "2";
                        sTemp += string.Format("发货成功！\r\n（注意:出库计划单 {0} 尚未完全发货）", queryOutPlanBill.Id);
                    }
                    else
                    {
                        //表示发货计划已完成
                        queryOutPlanBill.BillStatus = "3";
                        sTemp += "发货成功！";
                    }

                    //调整库存，根据出库单明细调整库存
                    if (client.UpdateOutPlanBill(queryOutPlanBill))
                    {
                        SysLogWriter.WriteOptLog("发货完成调整出库计划单状态", queryOutPlanBill.Id, "改");
                        foreach (BilOutBillDetail record in sendList)
                        {
                            //判断发货列表中的项与发货计划中的对应项，根据货物编码进行联系
                            StoGoodsStock stoRecord = new StoGoodsStock();
                            stoRecord.Id = record.StockCode;
                            stoRecord = client.QueryGoodsStock(stoRecord);
                            if (stoRecord != null)
                            {
                                stoRecord.PackageQty -= record.OutGoodsPackageQty;
                                stoRecord.PieceQty -= record.OutGoodsPieceQty;
                                stoRecord.GoodsTotalQty = stoRecord.PackageQty * stoRecord.GoodsSpec + stoRecord.PieceQty;
                                if (stoRecord.PackageQty == 0 && stoRecord.PieceQty == 0)
                                {
                                    if (!client.DeleteGoodsStock(stoRecord))
                                    {
                                        MessageBox.Show("更新库存失败！");
                                    }
                                }
                                else
                                {
                                    if (!client.UpdateGoodsStock(stoRecord))
                                    {
                                        MessageBox.Show("更新库存失败！");
                                    }
                                }
                            }
                        }
                        //如果为移库出库，则生成入库单
                        if (_outBill.OutType == "1")    //移库出库
                        {
                            BilInBill inBill = new BilInBill();
                            inBill.Id = client.InitInBillCode();
                            inBill.BillStatus = "1";
                            inBill.WarehouseCode = _outBill.RecieverCode;
                            inBill.WarehouseName = _outBill.RecieverName;
                            inBill.SenderCode = MainForm.CurrentUser.WarehouseCode;
                            inBill.SenderName = MainForm.CurrentUser.WarehouseName;
                            inBill.IBPlanCode = "";
                            inBill.IBType = "1";
                            DateTime dtNow = client.GetCurrentSystemTime();
                            inBill.ReceiveDate = string.Format("{0:yyyyMMdd}",dtNow);
                            inBill.ReceiveTime = string.Format("{0:hhmmss}",dtNow);
                            inBill.CreateDate = string.Format("{0:yyyyMMdd}", dtNow);
                            inBill.CreateTime = string.Format("{0:hhmmss}", dtNow);
                            inBill.CreateUserCode = "";
                            inBill.CreateUserName = "";
                            inBill.EditDate = "";
                            inBill.EditTime = "";
                            inBill.EditUserCode = "";
                            inBill.EditUserName = "";
                            inBill.IsDeactived = "0";

                            if (!client.AddInBill(inBill))
                            {
                                SysLogWriter.WriteOptLog("移库入库新建入库单", inBill.Id, "增");
                                MessageBox.Show("新建移库入库单失败！");
                            }
                            else
                            {
                                List<BilInBillDetail> inBillDetailList = new List<BilInBillDetail>();
                                foreach (BilOutBillDetail record in sendList)
                                {
                                    BilInBillDetail ibd = new BilInBillDetail();
                                    ibd.GoodsBarCode = record.GoodsBarCode;
                                    ibd.GoodsCode = record.GoodsCode;
                                    ibd.GoodsName = record.GoodsName;
                                    ibd.GoodsPackageQty = record.OutGoodsPackageQty;
                                    ibd.GoodsPieceQty = record.OutGoodsPieceQty;
                                    ibd.GoodsQty = record.OutGoodsQty;
                                    ibd.ReceiveGoodsPackageQty = record.OutGoodsPackageQty;
                                    ibd.ReceiveGoodsPieceQty = record.OutGoodsPieceQty;
                                    ibd.ReceiveGoodsQty = record.OutGoodsQty;
                                    ibd.GoodsSpec = record.GoodsSpec;
                                    ibd.IBCode = inBill.Id;
                                    ibd.PiecePrice = record.PiecePrice;
                                    ibd.SingleGoodsCode = record.SingleGoodsCode;

                                    inBillDetailList.Add(ibd);
                                    SysLogWriter.WriteOptLog("移库入库添加明细", string.Format("{0}-{1}",record.GoodsCode,record.OutGoodsQty), "增");
                                }

                                if (!client.AddInBillDetailList(inBillDetailList.ToArray()))
                                {
                                    MessageBox.Show("新建移库入库单明细失败！");
                                }
                            }
                        }

                        MessageBox.Show(sTemp);
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新出库计划失败！");
                    }
                    

                }
            }
        }

        private void nudQty_Enter(object sender, EventArgs e)
        {
            NumericUpDown nud = sender as NumericUpDown;
            nud.Select(0, nud.Value.ToString().Length);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
